package org.aeon.service.impl;


import lombok.extern.slf4j.Slf4j;
import org.aeon.dao.FriendApplyDao;
import org.aeon.dao.UserRelationDao;
import org.aeon.entity.FriendApply;
import org.aeon.entity.UserRelation;
import org.aeon.service.UserRelationService;
import org.aeon.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;
import java.util.Objects;


@Slf4j
public class UserRelationServiceImpl implements UserRelationService {


    @Override
    public boolean addFriends(FriendApply friendApply) {
        // 取消自动提交
        try (final SqlSession sqlSession = MybatisUtils.getSqlSession(false)) {
            final UserRelationDao mapper = sqlSession.getMapper(UserRelationDao.class);
            final FriendApplyDao friendMapper = sqlSession.getMapper(FriendApplyDao.class);
            // 先查询关系：
            // 关系：好友
            if (friendApply.getAgree() == 1) {
                int change = 0;
                // 添加好友信息
                change += friendMapper.updateResult(friendApply);
                final UserRelation relation = new UserRelation(friendApply.getUserId(),
                        friendApply.getAnotherUserId(), true, new Date());
                change += mapper.insert(relation);

                if (change == 2) sqlSession.commit();
                else sqlSession.rollback();

                return true;
            }
            return false;
        }
    }

    @Override
    public boolean blacklisted(Long userId, Long userId2) {
        // 取消自动提交
        try (final SqlSession sqlSession = MybatisUtils.getSqlSession(false)) {
            final UserRelationDao mapper = sqlSession.getMapper(UserRelationDao.class);
            // 先查询关系：
            UserRelation relation = mapper.getByTwoUserId(userId, userId2);
            if (relation == null || !Objects.equals(relation.getUserId(), userId)) {
                relation = new UserRelation(userId, userId2, false, new Date());

                // 添加黑名单
                if (mapper.insert(relation) == 1) sqlSession.commit();
                else sqlSession.rollback();

                return true;
            } else if (relation.getIsFriend()) {
                int change = 0;

                // 移除好友关系
                change += mapper.delById(relation.getId());

                relation.setIsFriend(false);
                relation.setStartTime(new Date());
                // 添加单向黑名单
                change += mapper.insert(relation);

                if (change == 2) sqlSession.commit();
                else sqlSession.rollback();

                return true;
            }
            // 黑名单：返回失败
            return false;
        }
    }

    @Override
    public boolean disassociate(Long userId, Long userId2) {
        try (final SqlSession sqlSession = MybatisUtils.getSqlSession(true)) {
            final UserRelationDao mapper = sqlSession.getMapper(UserRelationDao.class);
            final UserRelation relation = mapper.getByTwoUserId(userId, userId2);
            if (relation == null) return false;
            return mapper.delById(relation.getId()) == 1;
        }
    }

    @Override
    public UserRelation queryRelation(Long userId, Long userId2) {
        try (SqlSession sqlSession = MybatisUtils.getSqlSession(true)) {
            final UserRelationDao userRelationDao = sqlSession.getMapper(UserRelationDao.class);
            return userRelationDao.getByTwoUserId(userId, userId2);
        }
    }

    @Override
    public List<UserRelation> queryRelations(Long userId, Boolean isFriend) {
        try (SqlSession sqlSession = MybatisUtils.getSqlSession(true)) {
            final UserRelationDao userRelationDao = sqlSession.getMapper(UserRelationDao.class);
            return userRelationDao.getByUserIdAndFriend(userId, isFriend);
        }
    }
}
